home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Applications / Dice / Die1.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  6.8 KB  |  214 lines

  1. ∩╗┐using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7.  
  8. // Die component used in dice game - can be installed.
  9.  
  10. namespace Die
  11. {
  12.     /// <summary>
  13.     /// Summary description for UserControl1.
  14.     /// </summary>
  15.     public class Die : System.Windows.Forms.UserControl
  16.     {
  17.         /// <summary>
  18.         /// Required designer variable.
  19.         /// </summary>
  20.         private System.ComponentModel.Container components = null;
  21.         private System.Windows.Forms.PictureBox pictureBox1;
  22.         private System.Windows.Forms.PictureBox pictureBox3;
  23.         private System.Windows.Forms.PictureBox pictureBox4;
  24.         private System.Windows.Forms.PictureBox pictureBox5;
  25.         private System.Windows.Forms.PictureBox pictureBox6;
  26.         private System.Windows.Forms.PictureBox pictureBox7;
  27.         private System.Windows.Forms.PictureBox pictureBox9;
  28.  
  29.         // Specifies the random engine
  30.         // to launch the dice
  31.         private System.Random random;
  32.  
  33.         // Determines the number displayed
  34.         // on the dice from 1 to 6
  35.         private System.Int32 _face;
  36.         public System.Int32 face {
  37.             get { return _face; }
  38.             set {
  39.                 if (value>=1 && value<=6) {
  40.                     _face=value;
  41.                     display();
  42.                 } else {
  43.                     throw new Exception("Dice must be from 1 to 6");
  44.                 }
  45.             }
  46.         }
  47.  
  48.         // Allows of not the dice to be launched
  49.         private System.Boolean _active=true;
  50.         public System.Boolean active {
  51.             get { return _active; }
  52.             set {
  53.                 _active=value;
  54.                 // And changes dice background color
  55.                 BackColor=_active?
  56.                      System.Drawing.SystemColors.ActiveCaptionText
  57.                     :System.Drawing.SystemColors.InactiveBorder;
  58.             }
  59.         }
  60.  
  61.         public void launch()
  62.         {
  63.             if (active) {
  64.                 // Get a random face number
  65.                 face=random.Next(6)+1;
  66.             }
  67.         }
  68.  
  69.         public void display()
  70.         {
  71.             // Show/Hide points
  72.             pictureBox1.Visible=(face>1);
  73.             pictureBox3.Visible=(face>=4);
  74.             pictureBox4.Visible=(face==6);
  75.             pictureBox5.Visible=(face==1 || face==3 || face==5);
  76.             pictureBox6.Visible=(face==6);
  77.             pictureBox7.Visible=(face>=4);
  78.             pictureBox9.Visible=(face>1);
  79.         }
  80.  
  81.         public Die()
  82.         {
  83.             // This call is required by the Windows.Forms Form Designer.
  84.             random=new System.Random();
  85.             InitializeComponent();
  86.             launch();
  87.             // TODO: Add any initialization after the InitForm call
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Clean up any resources being used.
  92.         /// </summary>
  93.         protected override void Dispose( bool disposing )
  94.         {
  95.             if( disposing )
  96.             {
  97.                 if( components != null )
  98.                     components.Dispose();
  99.             }
  100.             base.Dispose( disposing );
  101.         }
  102.  
  103.         #region Component Designer generated code
  104.         /// <summary>
  105.         /// Required method for Designer support - do not modify 
  106.         /// the contents of this method with the code editor.
  107.         /// </summary>
  108.         private void InitializeComponent()
  109.         {
  110.             System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Die));
  111.             this.pictureBox1 = new System.Windows.Forms.PictureBox();
  112.             this.pictureBox3 = new System.Windows.Forms.PictureBox();
  113.             this.pictureBox4 = new System.Windows.Forms.PictureBox();
  114.             this.pictureBox5 = new System.Windows.Forms.PictureBox();
  115.             this.pictureBox6 = new System.Windows.Forms.PictureBox();
  116.             this.pictureBox7 = new System.Windows.Forms.PictureBox();
  117.             this.pictureBox9 = new System.Windows.Forms.PictureBox();
  118.             this.SuspendLayout();
  119.             // 
  120.             // pictureBox1
  121.             // 
  122.             this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
  123.             this.pictureBox1.Location = new System.Drawing.Point(8, 56);
  124.             this.pictureBox1.Name = "pictureBox1";
  125.             this.pictureBox1.Size = new System.Drawing.Size(16, 16);
  126.             this.pictureBox1.TabIndex = 0;
  127.             this.pictureBox1.TabStop = false;
  128.             this.pictureBox1.Visible = false;
  129.             // 
  130.             // pictureBox3
  131.             // 
  132.             this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
  133.             this.pictureBox3.Location = new System.Drawing.Point(56, 56);
  134.             this.pictureBox3.Name = "pictureBox3";
  135.             this.pictureBox3.Size = new System.Drawing.Size(16, 16);
  136.             this.pictureBox3.TabIndex = 2;
  137.             this.pictureBox3.TabStop = false;
  138.             this.pictureBox3.Visible = false;
  139.             // 
  140.             // pictureBox4
  141.             // 
  142.             this.pictureBox4.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox4.Image")));
  143.             this.pictureBox4.Location = new System.Drawing.Point(8, 32);
  144.             this.pictureBox4.Name = "pictureBox4";
  145.             this.pictureBox4.Size = new System.Drawing.Size(16, 16);
  146.             this.pictureBox4.TabIndex = 3;
  147.             this.pictureBox4.TabStop = false;
  148.             this.pictureBox4.Visible = false;
  149.             // 
  150.             // pictureBox5
  151.             // 
  152.             this.pictureBox5.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox5.Image")));
  153.             this.pictureBox5.Location = new System.Drawing.Point(32, 32);
  154.             this.pictureBox5.Name = "pictureBox5";
  155.             this.pictureBox5.Size = new System.Drawing.Size(16, 16);
  156.             this.pictureBox5.TabIndex = 4;
  157.             this.pictureBox5.TabStop = false;
  158.             this.pictureBox5.Visible = false;
  159.             // 
  160.             // pictureBox6
  161.             // 
  162.             this.pictureBox6.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox6.Image")));
  163.             this.pictureBox6.Location = new System.Drawing.Point(56, 32);
  164.             this.pictureBox6.Name = "pictureBox6";
  165.             this.pictureBox6.Size = new System.Drawing.Size(16, 16);
  166.             this.pictureBox6.TabIndex = 5;
  167.             this.pictureBox6.TabStop = false;
  168.             this.pictureBox6.Visible = false;
  169.             // 
  170.             // pictureBox7
  171.             // 
  172.             this.pictureBox7.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox7.Image")));
  173.             this.pictureBox7.Location = new System.Drawing.Point(8, 8);
  174.             this.pictureBox7.Name = "pictureBox7";
  175.             this.pictureBox7.Size = new System.Drawing.Size(16, 16);
  176.             this.pictureBox7.TabIndex = 6;
  177.             this.pictureBox7.TabStop = false;
  178.             this.pictureBox7.Visible = false;
  179.             // 
  180.             // pictureBox9
  181.             // 
  182.             this.pictureBox9.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox9.Image")));
  183.             this.pictureBox9.Location = new System.Drawing.Point(56, 8);
  184.             this.pictureBox9.Name = "pictureBox9";
  185.             this.pictureBox9.Size = new System.Drawing.Size(16, 16);
  186.             this.pictureBox9.TabIndex = 8;
  187.             this.pictureBox9.TabStop = false;
  188.             this.pictureBox9.Visible = false;
  189.             // 
  190.             // Dice
  191.             // 
  192.             this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
  193.             this.Controls.Add(this.pictureBox9);
  194.             this.Controls.Add(this.pictureBox7);
  195.             this.Controls.Add(this.pictureBox6);
  196.             this.Controls.Add(this.pictureBox5);
  197.             this.Controls.Add(this.pictureBox4);
  198.             this.Controls.Add(this.pictureBox3);
  199.             this.Controls.Add(this.pictureBox1);
  200.             this.Name = "Dice";
  201.             this.Size = new System.Drawing.Size(80, 80);
  202.             this.DoubleClick += new System.EventHandler(this.Dice_DoubleClick);
  203.             this.ResumeLayout(false);
  204.         }
  205.  
  206.         private void Dice_DoubleClick(object sender, System.EventArgs e)
  207.         {
  208.              active=!active;
  209.         }
  210.  
  211.         #endregion
  212.     }
  213. }
  214.